package com.sudo.sso.server.service.impl;

import cn.dev33.satoken.sso.exception.SaSsoException;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sudo.boot.base.constant.enums.EnumStatus;
import com.sudo.sso.server.entity.SysUser;
import com.sudo.sso.server.mapper.SysUserMapper;
import com.sudo.sso.server.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.sudo.sso.server.entity.table.SysUserTableDef.SYS_USER;

/**
 * 系统用户  服务层实现。
 *
 * @author admins
 * @since 2023-10-13
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public SysUser checkUserLogin(String username, String password) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_USER.ALL_COLUMNS)
                .where(SYS_USER.USERNAME.eq(username));
        SysUser sysUser = mapper.selectOneByQuery(queryWrapper);
        // 判断用户是否存在
        if (sysUser == null) {
            throw new SaSsoException("用户不存在");
        }
        // 判断密码是否正确
        if (!password.equals(sysUser.getPassword())) {
            throw new SaSsoException("密码不匹配");
        }
        // 判断用户状态
        if (!EnumStatus.ON.getCode().equals(sysUser.getStatus())) {
            throw new SaSsoException("用户已禁用");
        }
        return sysUser;
    }
}
